package com.itHsuSE.experiment;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Experiment8Server：基于TCP的服务器端程序
 * 实现了服务器监听和接收客户端传来的两个用户对象的功能。
 * 服务器监听在端口 12345，接收到客户端连接后，从输入流中读取两个用户对象并打印。
 * @author Hsu琛君珩
 * @version 1.0
 */
public class Experiment8Server {
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(12345)) {
            System.out.println("服务器监听在端口 12345...");

            while (true) {
                try (Socket clientSocket = serverSocket.accept();
                     ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream())) {

                    // 读取两个用户对象
                    User user1 = (User) ois.readObject();
                    User user2 = (User) ois.readObject();

                    // 打印接收到的用户信息
                    System.out.println("接收到的用户信息：");
                    System.out.println(user1);
                    System.out.println(user2);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
